const api = require('../../api/api')
const app = getApp()
const util = require('../../utils/js/util')
Page({
  data: {
    avatar: '',
    nickname: '',
    phone: '',
  },
  async onLoad() {
    this.setData({
      avatar: app.globalData.userInfo.avatar,
      nickname: app.globalData.userInfo.nickname,
      phone: app.globalData.userInfo.mobile,
    })
  },
  async submit() {
    const res = await api.profile({
      nickname: this.data.nickname,
      avatar: this.data.avatar,
      mobile: this.data.phone,
    })
    util.toast('修改成功')
  },
  chooseavatar(e) {
    wx.uploadFile({
      url: "https://jk.demo-dev.cn/api/common/upload",
      filePath: e.detail.avatarUrl,
      name: 'file',
      header: { "token": app.globalData.userInfo.token },
      success: res1 => {
        if (res1.statusCode === 200 && res1.errMsg === "uploadFile:ok") {
          const res2 = JSON.parse(res1.data)
          if (res2.code === 1) {
            util.toast(res2.msg)
            this.setData({ avatar: res2.data.fullurl })
          } else {
            util.toast(res2.msg)
          }
        }
      }
    })
  },
  async getphone(e) {
    const res = await api.getMobile({ code: e.detail.code})
    this.setData({ phone: res })
  },
})
